<template>
    <div class="main">
        <view
            class="canvasBox"
            style="
                width: 0;
                height: 0;
                overflow: hidden;
                opacity: 0;
                position: absolute;
                left: -750px;
                top: 0;
            "
        >
            <canvas canvas-id="customCanvas" type="2d"></canvas>
        </view>
        <div class="pleace">
            {{
                content.description +
                date.nongli +
                data.weekday +
                content.title +
                content.please
            }}1234567890年月日
        </div>
    </div>
</template>
 
<script>
import { mapState } from "vuex";
import drawPic from "../../utils/drawPic.js";
import { calendar } from "@/lib/calendar.js";
import dayjs from "@/lib/day.js";
import { chineseDate } from "@/lib/day.js";

export default {
    computed: {
        ...mapState({
            content: (state) => {
                console.log(state.xinyuqian.allXin.requestResult);
                const preview = state.xinyuqian.allXin.requestResult.looking;
                if (preview && preview.hasOwnProperty("_id")) {
                    return preview;
                } else {
                    state.xinyuqian.allXin.requestResult.looking =
                        state.xinyuqian.allXin.requestResult[0];
                    return state.xinyuqian.allXin.requestResult.looking;
                }
            },
        }),
        date() {
            if (!this.content) return {};
            let Time = new Date(this.content.date);
            if (isNaN(Time.getTime())) Time = new Date();
            const time = dayjs(Time);
            const date = time.toObject();
            const { IDayCn, IMonthCn } = calendar.solar2lunar(
                date.years,
                date.months + 1,
                date.date
            );
            return {
                ...date,
                nongli: IMonthCn + IDayCn,
                weekday: chineseDate.weekdaysShort[time.weekday()],
            };
        },
    },
    data() {
        return {
            px: 3,
        };
    },
    watch: {},
    mounted() {
        const query = this.createSelectorQuery();
        query
            .select("#customCanvas")
            .fields({ node: true, size: true })
            .exec((res) => {
                const canvas = res[0].node;
                this.draw(this.content, canvas);
            });
    },
    methods: {
        async draw(val, canvas) {
            var ctx = wx.createCanvasContext("customCanvas");
            await drawPic(ctx, { ...val, ...this.date }, this.px);
            const path = await new Promise((resolve, reject) => {
                setTimeout(function () {
                    wx.canvasToTempFilePath({
                        fileType: "png",
                        width: 400 * this.px,
                        height: 600 * this.px,
                        canvas,
                        success: function (res) {
                            var tempFilePath = res.tempFilePath;
                            resolve(tempFilePath);
                        },
                        fail: function (res) {
                            reject(res);
                        },
                    });
                }, 500);
            });
            this.saveImageToPhotosAlbum(path);
        },
        saveImageToPhotosAlbum(saveUrl) {
            return new Promise((resolve, reject) => {
                wx.saveImageToPhotosAlbum({
                    filePath: saveUrl,
                    success: (res) => {
                        wx.showToast({
                            title: "保存成功",
                            duration: 1000,
                        });
                        resolve();
                    },
                });
            });
        },
    },
};
</script>

<style lang="less" scoped>
.main {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: antiquewhite;
    height: 100vh;
}
canvas {
    width: 1200px;
    height: 1800px;
    background-color: white;
}
.pleace {
    position: absolute;
    font-family: Noto Serif SC;
    color: transparent;
    z-index: -1;
}
</style>